<template>
  <el-dialog
    :title="!dataForm.recordId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动id" prop="activityHid">
      <el-input v-model="dataForm.activityHid" placeholder="活动id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="已砍价格" prop="bargainBprice">
      <el-input v-model="dataForm.bargainBprice" placeholder="已砍价格"></el-input>
    </el-form-item>
    <el-form-item label="原价" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="原价"></el-input>
    </el-form-item>
    <el-form-item label="帮砍人数" prop="count">
      <el-input v-model="dataForm.count" placeholder="帮砍人数"></el-input>
    </el-form-item>
    <el-form-item label="现价" prop="bargainPrice">
      <el-input v-model="dataForm.bargainPrice" placeholder="现价"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="recordStatus">
      <el-input v-model="dataForm.recordStatus" placeholder="状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          recordId: 0,
          activityHid: '',
          goodsId: '',
          userId: '',
          bargainBprice: '',
          goodsPrice: '',
          count: '',
          bargainPrice: '',
          recordStatus: ''
        },
        dataRule: {
          activityHid: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          bargainBprice: [
            { required: true, message: '已砍价格不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '原价不能为空', trigger: 'blur' }
          ],
          count: [
            { required: true, message: '帮砍人数不能为空', trigger: 'blur' }
          ],
          bargainPrice: [
            { required: true, message: '现价不能为空', trigger: 'blur' }
          ],
          recordStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.recordId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.recordId) {
            this.$http({
              url: this.$http.adornUrl(`/kms/bargainrecord/info/${this.dataForm.recordId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.activityHid = data.bargainRecord.activityHid
                this.dataForm.goodsId = data.bargainRecord.goodsId
                this.dataForm.userId = data.bargainRecord.userId
                this.dataForm.bargainBprice = data.bargainRecord.bargainBprice
                this.dataForm.goodsPrice = data.bargainRecord.goodsPrice
                this.dataForm.count = data.bargainRecord.count
                this.dataForm.bargainPrice = data.bargainRecord.bargainPrice
                this.dataForm.recordStatus = data.bargainRecord.recordStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/kms/bargainrecord/${!this.dataForm.recordId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'recordId': this.dataForm.recordId || undefined,
                'activityHid': this.dataForm.activityHid,
                'goodsId': this.dataForm.goodsId,
                'userId': this.dataForm.userId,
                'bargainBprice': this.dataForm.bargainBprice,
                'goodsPrice': this.dataForm.goodsPrice,
                'count': this.dataForm.count,
                'bargainPrice': this.dataForm.bargainPrice,
                'recordStatus': this.dataForm.recordStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
